package com.literature.search.file.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import com.literature.search.core.entity.R.AjaxJson;
import com.literature.search.file.service.IElasticService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.elasticsearch.action.ingest.GetPipelineRequest;
import org.elasticsearch.action.ingest.GetPipelineResponse;
import org.elasticsearch.action.ingest.PutPipelineRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/**
 * @Author: DangKong
 * @Date: 2023/3/19 12:10
 * @Description:
 */
@Api(tags = "ES索引管理")
@RestController
@RequestMapping("/index")
@SaCheckLogin
@SaCheckRole(value = "admin")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))

public class IndexController {


    private final RestHighLevelClient client;

    private final IElasticService elasticService;




    @ApiOperation("是否存在索引")
    @PostMapping("/isExistsIndex")
    public AjaxJson existsIndex(@RequestParam String indexName) {
        Boolean existsIndex = elasticService.existsIndex(indexName);
        return AjaxJson.getByBoolean(existsIndex, "索引存在", "索引不存在");
    }


    @ApiOperation(value = "获取所有索引信息",notes = "返回一个Map,Map的key为索引名,value为索引的信息，目前只包括cn和en两个索引")
    @PostMapping("/getIndexInfo")
    public AjaxJson getIndexInfo() {
    return elasticService.getIndexInfo();
    }

    @ApiOperation(value = "创建pipline",hidden = true)
    @PostMapping("/createPipline")
    public AjaxJson createPipline() throws IOException {
        //获取Elasticsearch客户端
        String pipelineId = "my-pipeline"; // pipeline ID

        XContentBuilder builder = XContentFactory.jsonBuilder();
        builder.startObject();
        builder.field("description", "处理嵌套类型中的content字段");
        builder.startArray("processors");
        builder.startObject();
        builder.startObject("foreach");
        builder.field("field", "pageContents");
        builder.startObject("processor");
        builder.startObject("attachment");
        builder.field("field", "content");
        builder.endObject();
        builder.endObject();
        builder.endObject();
        builder.endObject();
        builder.startObject();
        builder.startObject("foreach");
        builder.field("field", "picContents");
        builder.startObject("processor");
        builder.startObject("attachment");
        builder.field("field", "content");
        builder.endObject();
        builder.endObject();
        builder.endObject();
        builder.endObject();
        builder.endArray();
        builder.endObject();


        //将builder转换为BytesReference
        BytesReference bytesReference = BytesReference.bytes(builder);
        PutPipelineRequest request = new PutPipelineRequest(pipelineId, bytesReference, XContentType.JSON);

        AcknowledgedResponse response = client.ingest().putPipeline(request, RequestOptions.DEFAULT);
        boolean acknowledged = response.isAcknowledged(); // 是否成功
        System.out.println(acknowledged);
        String s = response.toString();
        System.out.println(s);



        return null;
    }

    @ApiOperation(value = "查看所有的pipline",hidden = true)
    @PostMapping("/getPipline")
    public AjaxJson getPipline() throws IOException {

        GetPipelineRequest request = new GetPipelineRequest("my-pipeline");

        try {
            GetPipelineResponse response = client.ingest().getPipeline(request, RequestOptions.DEFAULT);
            String pipeline = response.pipelines().get(0).toString();
            System.out.println(pipeline);
        } catch (IOException e) {
            e.printStackTrace();
        }
        client.close();
        return null;
    }

    @ApiOperation(value = "一键删除所有索引",notes = "需要管理员账号,所有es中的数据，建议别用",tags = "admin",hidden = true)
    @SaCheckRole(value = "admin")
    @GetMapping("/deleteIndex")
    public AjaxJson deleteIndex() {
        Boolean aBoolean = elasticService.deleteIndex();
        return AjaxJson.getSuccessData(aBoolean);
    }
}


